package com.starbaba.baidublack.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.base.common.util.StringUtils;
import com.starbaba.baidublack.model.BlackInfo;
import com.starbaba.baidublack.service.BaiduBlackService;
import com.starbaba.core.web.page.PageRequest;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;

@SuppressWarnings({ "rawtypes", "deprecation" })
public class BaiduBlackServiceImpl implements BaiduBlackService {

	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);

	@Override
	public List<BlackInfo> getBaiduBlackList(PageRequest pageRequest, PageResponse pageResponse,
			Map<String, Object> paramsMap) {
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		List<BlackInfo> dataList = new ArrayList<BlackInfo>();
		StringBuffer strSql = new StringBuffer();
		strSql.append("SELECT a.id,a.userid,a.mobnum,a.platform,a.prd_id,a.createtime,a.checktime,a.comefrom,a.blacklevel,");
		strSql.append("b.channel_name,c.name as pdname ");
		strSql.append("FROM loan_user_black a LEFT JOIN comm_product_channel b ON (a.channel = b.channel_code) ");
		strSql.append("LEFT JOIN comm_product_prdid c ON(a.prd_id=c.prdid) ");
		strSql.append("WHERE 1=1");
		fillQueryCondition(paramsMap, strSql);
		strSql.append(" order by a." + pageRequest.getOrderby() + " ");
		strSql.append(pageRequest.getOrder());
		if (strSql.length() > 0 && start >= 0 && length > 0) {
			strSql.append(" LIMIT " + start + ", " + length);
		}
		try {
			dataList = dao.list(strSql.toString(), BlackInfo.class);
		} catch (AppException e) {
			e.printStackTrace();
		}

		StringBuffer countSql = new StringBuffer();
		countSql.append("SELECT count(a.id) ");
		countSql.append("FROM loan_user_black a LEFT JOIN comm_product_channel b ON (a.channel = b.channel_code) ");
		countSql.append("WHERE 1=1");
		fillQueryCondition(paramsMap, countSql);
		try {
			int count = dao.getInt(countSql.toString());
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (AppException e) {
			LogService.error(e);
		}
		return dataList;
	}


	@Override
	public String exportBaiduBlackCVS(List<BlackInfo> list) {
		StringBuffer sf=new StringBuffer();
		sf.append("用户id,手机号,平台,激活产品,激活渠道,注册时间,验证时间,验证来源,验证结果\r\n");
		for (BlackInfo vo : list) {
				sf.append("`"+vo.getUserid()).append(",");
			if(!StringUtils.isEmpty(vo.getMobnum())){
				sf.append("`"+vo.getMobnum()).append(",");
			}else{
				sf.append("--").append(",");
			}
			if(!StringUtils.isEmpty(vo.getPlatform())){
				sf.append(vo.getPlatform()).append(",");
			}else{
				sf.append("--").append(",");
			}
			if(!StringUtils.isEmpty(vo.getPdname())){
				sf.append(vo.getPdname()).append(",");
			}else{
				sf.append("--").append(",");
			}
			if(!StringUtils.isEmpty(vo.getChannel_name())){
				sf.append(vo.getChannel_name()).append(",");
			}else{
				sf.append("--").append(",");
			}
			if(!StringUtils.isEmpty(vo.getCreatetime())){
				sf.append(vo.getCreatetime()).append(",");
			}else{
				sf.append("--").append(",");
			}
			if(!StringUtils.isEmpty(vo.getChecktime())){
				sf.append(vo.getChecktime()).append(",");
			}else{
				sf.append("没有检查").append(",");
			}
			if(!StringUtils.isEmpty(vo.getComefrom())){
				sf.append(vo.getComefrom()).append(",");
			}else{
				sf.append("--").append(",");
			}
			if(!StringUtils.isEmpty(vo.getBlacklevel())){
				if("A".equals(vo.getBlacklevel())){
					sf.append(vo.getBlacklevel()+"级极黑用户").append(",");
				}
				if("B".equals(vo.getBlacklevel())){
					sf.append(vo.getBlacklevel()+"级高危用户").append(",");
				}
				if("C".equals(vo.getBlacklevel())){
					sf.append(vo.getBlacklevel()+"级值得关注用户").append(",");
				}
				if("D".equals(vo.getBlacklevel())){
					sf.append(vo.getBlacklevel()+"级有黑名单风险").append(",");
				}	
			}else{
				sf.append("信用良好").append(",");
			}
			sf.append("\r\n");
		}
		return sf.toString();
	}

	public void fillQueryCondition(Map<String, Object> paramsMap, StringBuffer strSql) {
		String platform = (String) paramsMap.get("platform");
		String prd_id = (String) paramsMap.get("prd_id");
		String channel = (String) paramsMap.get("channel");
		String userid = (String) paramsMap.get("userid");
		String mobnum = (String) paramsMap.get("mobnum");
		String comefrom = (String) paramsMap.get("comefrom");
		if (!StringUtils.isEmpty(platform)) {
			strSql.append(" and a.platform='" + platform + "'");
		}
		if (!StringUtils.isEmpty(prd_id)) {
			strSql.append(" and a.prd_id='" + prd_id + "'");
		}
		if (!StringUtils.isEmpty(channel)) {
			strSql.append(" and a.channel='" + channel + "'");
		}
		if (!StringUtils.isEmpty(userid)) {
			strSql.append(" and a.userid='" + userid + "'");
		}
		if (!StringUtils.isEmpty(mobnum)) {
			strSql.append(" and a.mobnum='" + mobnum + "'");
		}
		if (!StringUtils.isEmpty(comefrom)) {
			strSql.append(" and a.comefrom='" + comefrom + "'");
		}
	}

}
